
package nothing.dao.hibernate;

import java.sql.Date;
import nothing.model.Food;
import nothing.dao.FoodDao;
import org.appfuse.dao.hibernate.GenericDaoHibernate;

import java.util.List;
import nothing.model.Diary;

public class FoodDaoHibernate extends GenericDaoHibernate<Food, Long> implements FoodDao {

    public FoodDaoHibernate() {
        super(Food.class);
    }

    @SuppressWarnings("unchecked")
    public List findByDate(Date date) {
        List list = getHibernateTemplate().find(
                "from Food f, Diary d, Ingredient i where i.recipe =d.recipe and  f = i.food and d.when = ?", date);
        return list;
    }

    @SuppressWarnings("unchecked")
    public List findByDateRange(Date from,Date to) {
        Object[] objects = {from,to,from,to};
        List list = getHibernateTemplate().find(
                "from Food f, Diary d, Ingredient i where i.recipe =d.recipe " +
                "and  f = i.food and ((d.when > ? and d.when  < ?) or d.when = ? or d.when = ?)", objects);
        return list;
    }

    @SuppressWarnings("unchecked")
    public List<Food> findByName(String name) {
        return getHibernateTemplate().find("from Food where name=?", name);
    }

}
